package kz.pdt12.hw.framework;

import kz.pdt12.hw.data.GroupData;
import kz.pdt12.hw.utils.SortedListOf;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import java.util.List;

import static org.testng.Assert.assertEquals;

/**
 * Created with IntelliJ IDEA.
 * User: raimbek
 * Date: 07.03.13
 * Time: 0:45
 */
public class GroupHelper extends WebDriverHelperBase {

    public GroupHelper(ApplicationManager manager) {
        super(manager);
    }

    public GroupHelper createGroup(GroupData group) {
        manager.navigateTo().mainPage().groupsPage();

        initGroupCreation();
        fillGroupForm(group);
        submitGroupCreation();
        returnToGroupsPage();
        //update model
        manager.getModel().addGroup(group);
        return this;
    }


    public GroupHelper modifyGroup(int index, GroupData group) {
        initGroupModification(index);
        checkGroupWithDB();
        fillGroupForm(group);
        submitGroupModification();
        returnToGroupsPage();
        manager.getModel().removeGroup(index).addGroup(group);
        return this;
    }

    /**
     * Проверка соответствие форм группы с данными в базе данных
     */
    private void checkGroupWithDB() {
        GroupData group = new GroupData();
        fillGroupForm(group);
        GroupData groupDb = manager.getHibernateHelper().getGroup(group.getId());
        assertEquals(group.getName(), groupDb.getName());
        assertEquals(group.getFooter(), groupDb.getFooter());
        assertEquals(group.getHeader(), groupDb.getHeader());
    }

    public GroupHelper deleteGroup(int index) {
        selectGroupByIndex(index);
        submitGroupDeletion();
        returnToGroupsPage();
        manager.getModel().removeGroup(index);
        return this;
    }

    public GroupHelper initGroupCreation() {
        manager.navigateTo().groupsPage();
        driver.findElement(By.name("new")).click();
        return this;
    }

    public GroupHelper fillGroupForm(GroupData group) {
        By locatorGroupId = By.name("id");
        By locatorGroupName = By.name("group_name");
        By locatorGroupHeader = By.name("group_header");
        By locatorGroupFooter = By.name("group_footer");

        type(locatorGroupName, group.getName());
        type(locatorGroupHeader, group.getHeader());
        type(locatorGroupFooter, group.getFooter());

        if (group.getId() == null) {
            group.setId(getTextElement(locatorGroupId));
        }

        if (group.getName() == null) {
            group.setName(getTextElement(locatorGroupName));
        }
        if (group.getHeader() == null) {
            group.setHeader(getTextElement(locatorGroupHeader));
        }
        if (group.getFooter() == null) {
            group.setFooter(getTextElement(locatorGroupFooter));
        }
        return this;
    }

    public GroupHelper submitGroupCreation() {
        driver.findElement(By.name("submit")).click();
        return this;
    }

    public GroupHelper initGroupModification(int index) {
        selectGroupByIndex(index);
        click(By.name("edit"));
        return this;
    }

    private void selectGroupByIndex(int index) {
        click(By.cssSelector("input[name='selected[]']:nth-of-type(" + (index + 1) + ")"));
    }

    public GroupHelper submitGroupModification() {
        click(By.name("update"));
        return this;
    }

    public GroupHelper returnToGroupsPage() {
        driver.findElement(By.linkText("group page")).click();
        return this;
    }

    private void submitGroupDeletion() {
        click(By.name("delete"));
    }

    public SortedListOf<GroupData> getUiGroups() {
        SortedListOf<GroupData> groups = new SortedListOf<GroupData>();

        manager.navigateTo().groupsPage();

        List<WebElement> checkboxes = driver.findElements(By.name("selected[]"));
        for (WebElement checkbox : checkboxes) {
            GroupData group = new GroupData();
            String title = checkbox.getAttribute("title");
            group.setName(title.substring("Select (".length(), title.length() - ")".length()));
            groups.add(group);
        }

        return groups;
    }
}
